package com.cskaoyan.bean.vo.admin.auth;

import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: bwj
 * @Date: 2022/09/07/23:09
 * @Description: session处理器保持sessionid，跨域
 */
@Component
public class CustomShiroSessionManager extends DefaultWebSessionManager {
    //这个是和前端约定好sessionID从哪个请求头拿
    private static final String HEADER = "X-CskaoyanMarket-Admin-Token";
    private static final String MARKETHANDER = "X-CskaoyanMarket-Token";

    @Override
    protected Serializable getSessionId(ServletRequest servletRequest, ServletResponse servletResponse) {
        HttpServletRequest request= (HttpServletRequest) servletRequest;
        String sessionId = request.getHeader(HEADER);
        String sessionIdWx=request.getHeader(MARKETHANDER);
        if (sessionId!=null&&!"".equals(sessionId)){
            return sessionId;
        } else if (sessionIdWx != null &&! "".equals(sessionIdWx)) {
            return sessionIdWx;
        }else {
            return super.getSessionId(servletRequest, servletResponse);
        }


    }
}
